package info.ask4research.web;

import info.ask4research.model.UnitOfLearning;
import info.ask4research.model.activities.OneToTwoActivity;
import info.ask4research.model.roles.Role;
import info.ask4research.service.IActivityService;
import info.ask4research.service.IRoleService;
import info.ask4research.service.IUnitOfLearningService;
import info.ask4research.service.impl.RoleService;
import info.ask4research.service.impl.UnitOfLearningService;
import info.ask4research.web.dto.RoleDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RoleController {

	private IRoleService roleService;
	private IActivityService activityService;
	private IUnitOfLearningService unitOfLearningService;

	@RequestMapping(value="jsp/role/learner.action")
	public @ResponseBody Map learnerRole(@RequestParam(value="cmd") String cmd,
			@RequestParam(value="role") String stringRole,
			HttpSession session) throws Exception {

		UnitOfLearning unitOfLearning = unitOfLearningService.get((Integer)session.getAttribute("unitOfLearningId"));
		boolean roleExists = roleService.roleNameExists(stringRole,(Integer)session.getAttribute("unitOfLearningId"));
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		if(unitOfLearning != null && roleExists==false){
			Role role = new Role();
			role.setText(stringRole);
			role.setUnitOfLearning(unitOfLearning);
			role.setLearnerRole(true);
			role.setStaffRole(false);
			role.setMaxPersons(0);
			role.setMinPersons(0);
			role = roleService.save(role);
			List<Role> listRoles = roleService.getAll();
			modelMap.put("total", 1);
			modelMap.put("root", listRoles);
			modelMap.put("roleId", role.getId());
			modelMap.put("createNewRole", true);
			modelMap.put("success", true);
		} else {
			modelMap.put("total", 0);
			modelMap.put("data", null);
			modelMap.put("success", false);
		}

		return modelMap;
	}
	
	
	@RequestMapping(value="jsp/role/OneToTwoRole.action")
	public @ResponseBody Map OneToTwoRole(@RequestParam(value="cmd") String cmd,
			@RequestParam(value="activityId") String activityId,
			@RequestParam(value="user1") String user1,	
			HttpSession session) throws Exception {

		UnitOfLearning unitOfLearning = unitOfLearningService.get((Integer)session.getAttribute("unitOfLearningId"));
		boolean roleExists = roleService.roleNameExists(user1,(Integer)session.getAttribute("unitOfLearningId"));
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		if(unitOfLearning != null && roleExists==true){
			Role role = roleService.getRoleByName(user1);
			String[] activity = activityId.split(":");
			OneToTwoActivity oneToTwoActivity = new OneToTwoActivity();
			oneToTwoActivity = (OneToTwoActivity) activityService.getbyGraphId(activity[1]);
			if(oneToTwoActivity!=null){
				oneToTwoActivity.setSelectedUser(role);
				oneToTwoActivity = (OneToTwoActivity) activityService.save(oneToTwoActivity);
			}else{
				oneToTwoActivity = new OneToTwoActivity();
				oneToTwoActivity.setSelectedUser(role);
				oneToTwoActivity = (OneToTwoActivity) activityService.save(oneToTwoActivity);
			}
			modelMap.put("total", 1);
			modelMap.put("root", oneToTwoActivity);
			modelMap.put("roleId", oneToTwoActivity.getId());
			modelMap.put("success", true);
		} else {
			modelMap.put("total", 0);
			modelMap.put("data", null);
			modelMap.put("success", false);
		}

		return modelMap;
	}


	@RequestMapping(value="jsp/role/getTreeRoles.action")
	public @ResponseBody JSON treeLearnerRoles(@RequestParam(value="node") String node,
			HttpSession session) throws Exception {
		List<Role> listRoles = new ArrayList<Role>();
		List<Role> listRolesStaff = new ArrayList<Role>();
		List<Role> listRolesLearner = new ArrayList<Role>();
		int unitOfLearningId = (Integer)session.getAttribute("unitOfLearningId");

		JSON jsonObject = null;
		List<RoleDTO> rolesDTOs = new ArrayList<RoleDTO>();
		if(String.valueOf(unitOfLearningId) != null){
			if("staffLeaf".equals(node) || "Roles/Staff".equals(node)){
				listRoles = roleService.getStaffRolesByUoL(unitOfLearningId);
			} else if("learnerLeaf".equals(node) || "Roles/Learner".equals(node)){
				listRoles = roleService.getLearnerRolesByUoL(unitOfLearningId);
			} else {

				RoleDTO roleDTOLeaner = new RoleDTO();
				roleDTOLeaner.setText("Learner");
				roleDTOLeaner.setId("Roles/Learner");
				roleDTOLeaner.setCls("folder");
				roleDTOLeaner.setIconCls("learner");
				RoleDTO roleDTOStaff = new RoleDTO();
				roleDTOStaff.setText("Staff");
				roleDTOStaff.setId("Roles/Staff");
				roleDTOStaff.setCls("folder");
				roleDTOStaff.setIconCls("staff");
				rolesDTOs.add(roleDTOLeaner);
				rolesDTOs.add(roleDTOStaff);
			}


			for(Role role : listRoles){
				RoleDTO roleDTO = new RoleDTO(role);
				rolesDTOs.add(roleDTO);
			}
			JsonConfig jsonConfig = new JsonConfig();
			jsonObject = JSONSerializer.toJSON(rolesDTOs, jsonConfig);
		}
		return jsonObject;
	}



	@RequestMapping(value="jsp/role/staff.action")
	public @ResponseBody Map staffRole(@RequestParam(value="cmd") String cmd,
			@RequestParam(value="role") String stringRole,
			HttpSession session) throws Exception {

		UnitOfLearning unitOfLearning = unitOfLearningService.get((Integer)session.getAttribute("unitOfLearningId"));
		boolean roleExists = roleService.roleNameExists(stringRole,(Integer)session.getAttribute("unitOfLearningId"));
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		if(unitOfLearning != null && roleExists==false){
			Role role = new Role();
			role.setText(stringRole);
			role.setUnitOfLearning(unitOfLearning);
			role.setLearnerRole(false);
			role.setStaffRole(true);
			role.setMaxPersons(0);
			role.setMinPersons(0);
			role = roleService.save(role);


			modelMap.put("total", 1);
			modelMap.put("roleId", role.getId());
			modelMap.put("createNewRole", true);
			modelMap.put("success", true);
		} else {
			modelMap.put("total", 0);
			modelMap.put("data", null);
			modelMap.put("success", false);
		}

		return modelMap;
	}

	@RequestMapping(value="jsp/role/getAttributes.action")
	public @ResponseBody Map getRoleAttributes(
			@RequestParam(value="roleToEdit") String stringRole,
			HttpSession session) throws Exception {

		int unitOfLearningId = (Integer)session.getAttribute("unitOfLearningId");
		boolean roleExists = roleService.roleNameExists(stringRole,unitOfLearningId);
		Map<String,Object> modelMap = new HashMap<String,Object>(3);



		if(String.valueOf(unitOfLearningId) != null && roleExists==true){
			Role role = roleService.getRoleByUoFandRoleName(stringRole,unitOfLearningId);
			modelMap.put("total", 1);
			modelMap.put("role", role);
			modelMap.put("success", true);
		} else {
			modelMap.put("total", 0);
			modelMap.put("data", null);
			modelMap.put("failure", false);
		}

		return modelMap;
	}


	@RequestMapping(value="jsp/role/saveAttributes.action")
	public @ResponseBody Map saveRoleAttributes(
			@RequestParam(value="allowCreation") boolean allowCreation,
			@RequestParam(value="information") String information,
			@RequestParam(value="maxPersons") String maxPersons,
			@RequestParam(value="minPersons") String minPersons,
			@RequestParam(value="notAssignToOther") boolean notAssignToOther,
			@RequestParam(value="text") String text,
			HttpSession session) throws Exception {

		int unitOfLearningId = (Integer)session.getAttribute("unitOfLearningId");
		boolean roleExists = roleService.roleNameExists(text,(Integer)session.getAttribute("unitOfLearningId"));
		Map<String,Object> modelMap = new HashMap<String,Object>(3);

		if(String.valueOf(unitOfLearningId) != null && roleExists==true){
			Role role = roleService.getRoleByUoFandRoleName(text,unitOfLearningId);
			role.setAllowCreation(allowCreation);
			if(!StringUtils.isEmpty(information)){
				role.setInformation(information);
			}else{
				role.setInformation("");
			}
			role.setMaxPersons(Integer.parseInt(maxPersons));
			role.setMinPersons(Integer.parseInt(minPersons));
			role.setNotAssignToOther(notAssignToOther);
			role.setText(text);
			role = roleService.save(role);
			modelMap.put("total", 1);
			modelMap.put("role", role);
			modelMap.put("success", true);
		} else {
			modelMap.put("total", 0);
			modelMap.put("data", null);
			modelMap.put("failure", false);
		}

		return modelMap;
	}


	public IUnitOfLearningService getUnitOfLearningService() {
		return unitOfLearningService;
	}
	@Resource(name="unitOfLearningService")
	public void setUnitOfLearningService(IUnitOfLearningService unitOfLearningService) {
		this.unitOfLearningService = unitOfLearningService;
	}
	public IRoleService getRoleService() {
		return roleService;
	}
	@Resource(name="roleService")
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	public IActivityService getActivityService() {
		return activityService;
	}
	@Resource(name="activityService")
	public void setActivityService(IActivityService activityService) {
		this.activityService = activityService;
	}
}
